//
//  WorkingView.m
//  hnGovernment
//
//  Created by 安仔 on 17/2/27.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "WorkingView.h"
#import "WorkingCell.h"
#import "SserviceByCategoryObj.h"

@interface WorkingView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation WorkingView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initCollectionView];
    }
    return self;
}

//创建承载微应用的collectionView
- (void)initCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    self.collectView.showsVerticalScrollIndicator = YES;
    self.collectView.showsHorizontalScrollIndicator = NO;
    self.collectView.backgroundColor = [UIColor whiteColor];
    
    //注册 colllectionCell
    [self.collectView registerClass:[WorkingCell class] forCellWithReuseIdentifier:@"WorkingCell"];
    [self addSubview:self.collectView];
}

#pragma mark- UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
   // NSArray *array = self.collectArray[@""]
    
    return self.collectArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WorkingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WorkingCell" forIndexPath:indexPath];
    [cell.titleLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(cell.centerY);
        make.left.mas_equalTo(cell.signImg.mas_right).offset(20*m6Scale);
    }];
    
    SserviceByCategoryObj *obj = self.collectArray[indexPath.item];
    cell.titleLab.text = obj.serviceName;
    [cell.signImg sd_setImageWithURL:[NSURL URLWithString:obj.serviceImg]];

    
    return cell;
}

#pragma mark- collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if ([self.delegate respondsToSelector:@selector(selectWorkingItem:)]) {
        [self.delegate selectWorkingItem:indexPath.row];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(KScreenWidth / 2, 142*m6Scale);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
}

@end
